<template>
  <el-dialog
      top="10vh"
      width="500px"
      title="添加/编辑"
      :visible.sync="dialogValue"
      :close-on-click-modal="false"
      append-to-body
  >
    <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @change="handleFormChange"
        @save="onSave"
        @close="dialogValue = false"
    />
  </el-dialog>
</template>

<script>
import {http} from '@/api'

export default {
  name: 'FormAdd',
  data() {
    return {
      apiKey: 'postComment',
      dialogValue: false,
      ruleForm: {},
      defaultForm: {
        post_id: '',
        user_id: '',
        parent_id: '',
        reply_to_user_id: '',
        content: '',
        status: 1
      },
      rules: {
        post_id: [{required: true, message: '帖子ID不能为空'}],
        user_id: [{required: true, message: '用户ID不能为空'}],
        content: [{required: true, message: '评论内容不能为空'}],
      },
      formList: [
        {
          label: '帖子',
          prop: 'post_id',
          name: 'select',
          colSpan: 24,
          placeholder: '请选择帖子',
          options: []
        },
        {
          label: '用户',
          prop: 'user_id',
          name: 'select',
          colSpan: 24,
          placeholder: '请选择用户',
          options: []
        },
        {
          label: '父评论ID',
          prop: 'parent_id',
          name: 'input',
          colSpan: 24,
          placeholder: '请输入父评论ID（选填）'
        },
        {
          label: '回复用户ID',
          prop: 'reply_to_user_id',
          name: 'input',
          colSpan: 24,
          placeholder: '请输入回复用户ID（选填）'
        },
        {
          label: '评论内容',
          prop: 'content',
          name: 'input',
          type: 'textarea',
          colSpan: 24,
          placeholder: '请输入评论内容',
          rows: 6
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          colSpan: 24,
          options: [
            { label: '正常', value: 1 },
            { label: '已删除', value: 0 },
            { label: '已屏蔽', value: 2 }
          ]
        },
      ]
    }
  },
  methods: {
    handleFormChange({key, val}) {
      // 处理表单值变化
    },
    async getUserList() {
      try {
        const res = await http.user.list()
        const userOptions = res.data.map(item => ({
          label: item.name || item.nickname,
          value: item.id
        }))
        this.formList.find(item => item.prop === 'user_id').options = userOptions
      } catch (error) {
        console.error('获取用户列表失败:', error)
      }
    },
    async getPostList() {
      try {
        const res = await http.post.list()
        const postOptions = res.data.map(item => ({
          label: item.title,
          value: item.id
        }))
        this.formList.find(item => item.prop === 'post_id').options = postOptions
      } catch (error) {
        console.error('获取帖子列表失败:', error)
      }
    },
    open(row) {
      this.dialogValue = true
      this.getUserList()
      this.getPostList()
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data,
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm
        }
      }
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, this.ruleForm).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
